package com.github.ecbp.user.service.controller.front;

import com.github.ecbp.common.api.CommonResult;
import com.github.ecbp.common.data.controller.BaseController;
import com.github.ecbp.common.security.utils.CommonAdminUtils;
import com.github.ecbp.common.utils.Asserts;
import com.github.ecbp.user.service.common.UserCodeAndMessageEnum;
import com.github.ecbp.user.service.dto.AdminLoginDto;
import com.github.ecbp.user.service.dto.ButtonBo;
import com.github.ecbp.user.service.dto.MenuBo;
import com.github.ecbp.user.service.dto.PhoneDto;
import com.github.ecbp.user.service.request.FindAdminPasswordReq;
import com.github.ecbp.user.service.service.AdminSaveService;
import com.github.ecbp.user.service.service.AdminService;
import com.github.ecbp.user.service.service.MenuService;
import com.github.ecbp.user.service.vo.ButtonNode;
import com.github.ecbp.user.service.vo.MenuNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.github.ecbp.common.constant.CacheKeyConstant.PROJECT_NAME;

/**
 * <p>文件名称: 登陆认证 </p>
 * <p>描述: 登陆认证 </p>
 *
 * @author zhangj
 * @date 2020/4/12 18:33
 */
@Api(tags = "登陆认证")
@RequestMapping("/api/" + PROJECT_NAME + "/user")
@RestController
public class LoginController extends BaseController {
    @Autowired
    protected AdminService adminService;
    @Autowired
    protected AdminSaveService adminSaveService;
    @Autowired
    protected MenuService menuService;

    @ApiOperation(value = "登录-获取图片验证码", produces = "image/gif", notes = "zhangj")
    @GetMapping(value = "/noauth/imageCode")
    public void getImageCode() {
        try {
            // 存储验证码
            adminService.saveImageCode();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @ApiOperation(value = "平台端登录", notes = "zhangj")
    @PostMapping(value = "/noauth/login")
    public CommonResult login(@Validated @RequestBody AdminLoginDto param) {
        String token = adminService.login(param);
        Asserts.isTrue(token != null, UserCodeAndMessageEnum.USERNAME_PASSWORD_ERROR);
        return CommonResult.succeed(Collections.singletonMap("token", token));
    }

    @ApiOperation(value = "获取当前登录用户信息", notes = "zhangj")
    @PostMapping(value = "/info")
    public CommonResult info() {
        Map<String, Object> data = new HashMap<>(4);
        data.put("loginPhone", CommonAdminUtils.getLoginPhoneFromToken());
        data.put("username", CommonAdminUtils.getUsernameFromSystem());
        data.put("icon", CommonAdminUtils.getIconFromSystem());
        return CommonResult.succeed(data);
    }

    @ApiOperation(value = "登出", notes = "zhangj")
    @PostMapping(value = "/logout")
    public CommonResult logout() {
        adminService.logout();
        return CommonResult.succeed(null);
    }

    @ApiOperation(value = "发送验证码", notes = "zhangj")
    @PostMapping(value = "/noauth/sendVerification")
    public CommonResult sendVerification(@Validated @RequestBody PhoneDto param) {
        String code = adminService.sendVerification(param);
        return CommonResult.succeed(code);
    }

    @ApiOperation(value = "忘记密码/找回密码", notes = "zhangj")
    @PostMapping(value = "/noauth/findPassword")
    public CommonResult findPassword(@Validated @RequestBody FindAdminPasswordReq param) {
        adminSaveService.findPassword(param);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "返回菜单下(当前用户)按钮", notes = "zhangj", response = ButtonNode.class)
    @PostMapping(value = "/buttonList/auth")
    public CommonResult buttonListAuth(Long menuId) {
        List<ButtonNode> list = menuService.buttonList(new ButtonBo(menuId, CommonAdminUtils.getUserDetailsFromSystem()));
        return CommonResult.succeed(list);
    }

    @ApiOperation(value = "树形结构返回(当前用户)菜单列表", notes = "zhangj", response = MenuNode.class)
    @PostMapping(value = "/treeList/auth")
    public CommonResult treeListAuth() {
        List<MenuNode> list = menuService.treeList(new MenuBo(false, CommonAdminUtils.getUserDetailsFromSystem()));
        return CommonResult.succeed(list);
    }

    @ApiOperation(value = "树形结构返回(当前用户)菜单列表+按钮", notes = "zhangj", response = MenuNode.class)
    @PostMapping(value = "/treeListWithButton/auth")
    public CommonResult treeListWithButtonAuth() {
        List<MenuNode> list = menuService.treeList(new MenuBo(true, CommonAdminUtils.getUserDetailsFromSystem()));
        return CommonResult.succeed(list);
    }
}
